package com.kly.user.db.dao.impl;

import com.fqgj.common.base.AbstractBaseMapper;
import com.google.common.collect.Maps;
import com.kly.enums.ParameterTypeEnum;
import com.kly.user.db.dao.UserTaskDetailDao;
import com.kly.user.db.dbo.PaidOrderDBO;
import com.kly.user.db.entity.UserTaskDetailEntity;
import com.kly.user.task.enums.TaskStatusEnum;
import com.kly.user.task.enums.TaskTypeEnum;
import org.springframework.stereotype.Repository;
import java.util.HashMap;
import java.util.List;

@Repository
public class UserTaskDetailDaoImpl extends AbstractBaseMapper<UserTaskDetailEntity> implements UserTaskDetailDao {

    @Override
    public UserTaskDetailEntity addTask(ParameterTypeEnum taskType, String request) {
        UserTaskDetailEntity taskDetailEntity = new UserTaskDetailEntity();
        taskDetailEntity.setRequest(request);
        taskDetailEntity.setStatus(TaskStatusEnum.INIT.getCode());
        taskDetailEntity.setTaskName(taskType.getCode());
        taskDetailEntity.setExecuteTimes(1);
        taskDetailEntity.setTaskCode(String.valueOf(taskType.getId()));
        return this.insert(taskDetailEntity);
    }

    @Override
    public UserTaskDetailEntity addHasExecutedTask(TaskTypeEnum taskType, TaskStatusEnum taskStatus, String request, String result) {
        UserTaskDetailEntity taskDetailEntity = new UserTaskDetailEntity();
        taskDetailEntity.setRequest(request);
        taskDetailEntity.setResponse(result);
        taskDetailEntity.setStatus(taskStatus.getCode());
        taskDetailEntity.setTaskName(taskType.getName());
        taskDetailEntity.setExecuteTimes(1);
        taskDetailEntity.setTaskCode(String.valueOf(taskType.getCode()));
        return this.insert(taskDetailEntity);
    }

    @Override
    public UserTaskDetailEntity addHasExecutedTask(TaskTypeEnum taskType, TaskStatusEnum taskStatus, String request, String result, String extend, String url, String taskDesc) {
        UserTaskDetailEntity taskDetailEntity = new UserTaskDetailEntity();
        taskDetailEntity.setRequest(request);
        taskDetailEntity.setResponse(result);
        taskDetailEntity.setStatus(taskStatus.getCode());
        taskDetailEntity.setTaskName(taskType.getName());
        taskDetailEntity.setExecuteTimes(0);
        taskDetailEntity.setTaskCode(String.valueOf(taskType.getCode()));
        taskDetailEntity.setExtend(extend);
        taskDetailEntity.setTaskDesc(taskDesc);
        taskDetailEntity.setCallbackUrl(url);
        return this.insert(taskDetailEntity);
    }

    @Override
    public int updateTask(Long id, Integer status, String result) {
        final HashMap<String, Object> params = Maps.newHashMap();
        params.put("id", id);
        params.put("status", status);
        params.put("response", result);
        return this.getSqlSession().update(this.getStatement(".updateTask"), params);
    }
    @Override
    public List<UserTaskDetailEntity> queryNeedProcessJobs(Integer maxExecuteTimes) {
        final HashMap<String, Object> params = Maps.newHashMap();
        params.put("num", maxExecuteTimes);
        return this.getSqlSession().selectList(this.getStatement("queryNeedProcessJobs"), params);
    }

    @Override
    public List<UserTaskDetailEntity> queryNeedProcessJobsByType(Integer taskType, Integer maxExecuteTimes) {
        final HashMap<String, Object> params = Maps.newHashMap();
        params.put("num", maxExecuteTimes);
        params.put("taskType", taskType);
        return this.getSqlSession().selectList(this.getStatement("queryNeedProcessJobsByType"), params);
    }

    @Override
    public List<UserTaskDetailEntity> listAllTaskLatestDay(Integer taskCode) {
        return this.getSqlSession().selectList(this.getStatement("listAllTaskLatestDay"), taskCode);
    }

    @Override
    public List<UserTaskDetailEntity> listPaidOrders(Integer hour, String eventName) {
        final HashMap<String, Object> params = Maps.newHashMap();
        params.put("eventName", eventName);
        return this.getSqlSession().selectList(this.getStatement("listPaidOrders"), params);
    }

    @Override
    public List<PaidOrderDBO> listAllPaidOrdersLatestDay() {
        return this.getSqlSession().selectList(this.getStatement("listAllPaidOrdersLatestDay"));
    }

    @Override
    public List<PaidOrderDBO> listAllPaidOrdersLatestDayFromAppflyers() {
        return this.getSqlSession().selectList(this.getStatement("listAllPaidOrdersLatestDayFromAppflyers"));
    }
}